/* * * created By Xu Peng * */
<template>
  <el-select
    style="width: 200px"
    :clearable="clearable"
    size="small"
    filterable
    remote
    :remote-method="remoteMethod"
    :loading="loading"
    v-model="data"
    @change="handleChange"
    v-bind="$attrs"
  >
    <el-option v-for="item in selectOptions" :key="item.code" :label="item.name" :value="item.code" />
  </el-select>
</template>

<script>
export default {
  props: {
    value: {
      type: [String, Array, Number, Boolean]
    },
    options: {
      type: Array,
      default: () => []
    },
    query: {
      type: String,
      default: ""
    },
    clearable: {
      type: Boolean,
      default: true
    },
    autoQuery: {
      type: Boolean,
      default: true
    },
    queryName: {
      type: String,
      default: "name"
    }
  },
  created() {
    this.selectOptions = this.options
  },
  data() {
    return {
      loading: false,
      data: this.value,
      selectOptions: []
    }
  },
  watch: {
    value(val) {
      this.data = val
    }
  },
  methods: {
    remoteMethod(query) {
      if (query !== "") {
        this.loading = true
        setTimeout(() => {
          this.loading = false
          const params = {
            [this.queryName]: query
          }
          this.$ajax.get(this.query, params).then((data) => {
            this.selectOptions = data.data
          })
        }, 200)
      } else {
        this.selectOptions = []
      }
    },
    handleChange(value) {
      this.$emit("input", value)
    }
  }
}
</script>
<style lang="less"></style>
